package org.esgi.downloader.impl;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.esgi.downloader.interfaces.IFormInfoDownloader;
import org.esgi.model.interfaces.IForm;
import org.esgi.model.interfaces.IFormInfo;
import org.esgi.transformer.impl.XMLAnswerTransformer;
import org.esgi.transformer.impl.XMLFormInfoTransformer;
import org.esgi.transformer.impl.XMLFormTransformer;
import org.esgi.transformer.impl.XMLQuestionTransformer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLFormDownloader extends XMLDownloader implements IFormInfoDownloader
{
	public static final String XMLFormAllFormURL = "http://localhost:8080/PPAScript/admin/getFormListWebService";
	public static final String XMLFormGetFormURL = "http://localhost:8080/PPAScript/admin/getFormWebService?form_id=";
	
	@Override
	public List<IFormInfo> loadAllFormInfo()
	{
		String xmlStr = this.loadURL(XMLFormAllFormURL);
		if(null != xmlStr){
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			Document doc = null;
			try {
				DocumentBuilder db = dbf.newDocumentBuilder();
				InputSource is = new InputSource(new StringReader(xmlStr));
		        doc = db.parse(is);

			}catch(ParserConfigurationException pce) {
				pce.printStackTrace();
			}catch(SAXException se) {
				se.printStackTrace();
			}catch(IOException ioe) {
				ioe.printStackTrace();
			}
			if(doc != null)
			{
				Element root = doc.getDocumentElement();
				NodeList nl = root.getChildNodes();
				ArrayList<IFormInfo> list = new ArrayList<IFormInfo>();
				XMLFormInfoTransformer transformer = new XMLFormInfoTransformer();
				for(int i = 0; i < nl.getLength(); i++){
					list.add(transformer.formInfoFromElement((Element)nl.item(i)));
				}				
				return list;
			}	
		}
		return null;
	}
	
	@Override
	public IForm loadFormWithId(String anId)
	{
		String xmlStr = this.loadURL(XMLFormGetFormURL + anId);
		if(null != xmlStr){
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			Document doc = null;
			try {
				DocumentBuilder db = dbf.newDocumentBuilder();
				InputSource is = new InputSource(new StringReader(xmlStr));
		        doc = db.parse(is);

			}catch(ParserConfigurationException pce) {
				pce.printStackTrace();
			}catch(SAXException se) {
				se.printStackTrace();
			}catch(IOException ioe) {
				ioe.printStackTrace();
			}
			if(doc != null)
			{
				Element root = doc.getDocumentElement();
				String namespace = "bmb";
				XMLAnswerTransformer answerTransformer = new XMLAnswerTransformer();
				answerTransformer.setDefaultNamespace(namespace);
				XMLQuestionTransformer questionTransformer = new XMLQuestionTransformer(answerTransformer);
				questionTransformer.setDefaultNamespace(namespace);
				XMLFormTransformer formTransformer = new XMLFormTransformer(questionTransformer);
				formTransformer.setDefaultNamespace(namespace);				
				return formTransformer.formFromElement(root);
			}	
		}
		return null;
	}
}
